package tr.edu.yildiz.ce.dap.acm;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.http.HttpService;
import org.osgi.util.tracker.ServiceTracker;

import tr.edu.yildiz.ce.dap.acm.servlet.DownloadServlet;

public class Activator implements BundleActivator {
	
	public static final String CONTEXT_PATH = "/download";
	ServiceTracker httpServiceTracker;

	public void start(BundleContext context) throws Exception {
//		httpServiceTracker = new ServiceTracker(context, HttpService.class.getName(), null){
//			@Override
//			public Object addingService(ServiceReference reference) {
//				HttpService httpService = (HttpService)super.addingService(reference);
//				try {
//					httpService.registerServlet(CONTEXT_PATH,new DownloadServlet(httpServiceTracker), null,null);
//				}catch (Exception e) {
//					e.printStackTrace();
//				}
//				return httpService;
//			}
//			
//			public void removedService(ServiceReference reference, Object service) {
//				((HttpService)service).unregister(CONTEXT_PATH);
//				super.removedService(reference, service);
//				
//			}
//		};
//
//		httpServiceTracker.open();
	      ServiceReference sRef = context.getServiceReference(HttpService.class.getName());
	      if (sRef != null)
	      {
	         HttpService service = (HttpService) context.getService(sRef);
	         service.registerServlet("/download", new DownloadServlet(), null, null);
	      }
	}

	public void stop(BundleContext context) throws Exception {
		httpServiceTracker.close();

	}

}
